import pos from '../../frame/location'
import {message} from 'antd'

let _map = null;

// 初始化地图，需要设置中心点
export const initPicker = (center) => {
    //加载PositionPicker，loadUI的路径参数为模块名中 'ui/' 之后的部分
    AMapUI.loadUI(['misc/PositionPicker'], (PositionPicker) => {
        let param = {};
        if (center) {
            param = {
                zoom: 16,
                center: center
            };
        } else {
            param = {
                zoom: 16
            }
        }
        let map = new AMap.Map('container', param)
        _map = map;

        let positionPicker = new PositionPicker({
            mode: 'dragMap', //设定为拖拽地图模式，可选'dragMap'、'dragMarker'，默认为'dragMap'
            map: map //依赖地图对象
        });
        //TODO:事件绑定、结果处理等
        positionPicker.on('success', (positionResult) => {
            message.success(`当前地址为：${positionResult.address}`, 1);
            T.data('Position_Picker_Result', positionResult);
        });
        positionPicker.on('fail', (positionResult) => {});
        let onModeChange = (e) => {
            positionPicker.setMode(e.target.value)
        }
        positionPicker.start();
    });
}

// 获得定位坐标和地址
export const getPosition = () => {
    let center = new Object();
    pos.getlocation((data) => {
        var result = JSON.parse(data);

        if (result.type === 'error') {
            console.log(data)
        } else {
            // baseInfo['patrollocation'] = result.formattedAddress;
            center = {
                latng: [
                    result.position.lng || "120.22222",
                    result.position.lat || "25.22222"
                ],
                address: result.formattedAddress
            }
        }
    });
    return center;
}